package com.ukao.cashregister.ui.vipManager;


import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.ukao.cashregister.R;
import com.ukao.cashregister.adapter.CouponAdapter;
import com.ukao.cashregister.adapter.RechargeRecordAdapter;
import com.ukao.cashregister.adapter.TradeRecordAdapter;
import com.ukao.cashregister.base.IAdapter;
import com.ukao.cashregister.base.IPage;
import com.ukao.cashregister.base.MvpFragment;
import com.ukao.cashregister.base.PageWrapper;
import com.ukao.cashregister.bean.Coupons;
import com.ukao.cashregister.bean.RechargeRecordBean;
import com.ukao.cashregister.bean.TradeRecordBean;
import com.ukao.cashregister.bean.UserInfoBean;
import com.ukao.cashregister.pesenter.ConsumeRecordPesenter;
import com.ukao.cashregister.utils.T;
import com.ukao.cashregister.view.ConsumeRecordView;
import com.ukao.cashregister.widget.TwinklingLayout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 消费记录
 */
public class ConsumeRecordFragment extends MvpFragment<ConsumeRecordPesenter>  implements ConsumeRecordView{

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.record_recycler_View)
    RecyclerView recordRecyclerView;
    @BindView(R.id.twinkling_layout)
    TwinklingLayout twinklingLayout;
    Unbinder unbinder;


    private String mParam1;
    private UserInfoBean.UserBean mUserBean;
    private PageWrapper mPageWrapper;

    private List<Coupons> items = new ArrayList<>();
    private List<RechargeRecordBean.ListBean> mRechargeRecordData = new ArrayList<>();
    private List<TradeRecordBean.ListBean> mTradeRecordData = new ArrayList<>();
    private RechargeRecordAdapter mRechargeRecordAdapter;
    private   int enterType;/*进入类型*/
    private CouponAdapter mCouponAdapter;
    private  boolean  isloadfinish;
    private String useId;
    private TradeRecordAdapter mTradeRecordAdapter;

    public static ConsumeRecordFragment newInstance(int isCouponEnter, Serializable param2) {
        ConsumeRecordFragment fragment = new ConsumeRecordFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, isCouponEnter);
        args.putSerializable(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            enterType = getArguments().getInt(ARG_PARAM1);
            mUserBean = (UserInfoBean.UserBean)getArguments().getSerializable(ARG_PARAM2);
        }
    }

    @Override
    protected ConsumeRecordPesenter createPresenter() {
        return new ConsumeRecordPesenter(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_consume_record, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initListener();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if (!isloadfinish)
            switch (enterType) {
                case 3:
                    twinklingLayout.startRefresh();
                    twinklingLayout.setEnableLoadmore(false);
                    break;
                default:
                    twinklingLayout.startRefresh();
                    break;
            }
    }

    @Override
    protected void initView() {
        isloadfinish=false;
        useId=mUserBean.getInfo().getId();
       IPage page = new IPage() {
        @Override
        public void load(int pageIndex, int pageSize) {
            switch (enterType) {
                case 2:

                    mvpPresenter.userRecharge(useId,pageIndex,pageSize);
                    break;
                case 1:
                    mvpPresenter.tradeOrder(useId,pageIndex,pageSize);
                    break;

            }
        }
    };


    initLinearRecyclerView(recordRecyclerView);
        switch (enterType) {
            case 3:
                mCouponAdapter = new CouponAdapter(activity, items);
                recordRecyclerView.setAdapter(mCouponAdapter);
                break;
            case 2:
                mRechargeRecordAdapter = new RechargeRecordAdapter(activity, mRechargeRecordData);
                recordRecyclerView.setAdapter(mRechargeRecordAdapter);
                break;
            case 1:
                mTradeRecordAdapter= new TradeRecordAdapter(activity, mTradeRecordData);
                recordRecyclerView.setAdapter(mTradeRecordAdapter);
                break;
        }

        mPageWrapper= new PageWrapper<>(mIAdapter, page);

}




    IAdapter mIAdapter = new IAdapter() {
        @Override
        public void setDataSource(List data, boolean isNextLoad) {

            switch (enterType) {
                case 2:
                    mRechargeRecordAdapter.setDatas(data);
                    break;
                default:
                    mTradeRecordAdapter.setDatas(data);
                    break;
            }


            twinklingLayout.setEnableLoadmore(isNextLoad);

        }

        @Override
        public void loadMoreFinish(List data, boolean isNextLoad) {
            switch (enterType) {
                case 2:
                    mRechargeRecordAdapter.addDatas(data);
                    break;
                default:
                    mTradeRecordAdapter.addDatas(data);
                    break;
            }

            twinklingLayout.setEnableLoadmore(isNextLoad);

        }
    };

    @Override
    protected void initListener() {
        super.initListener();

        twinklingLayout.setOnRefreshListener(mRefreshListenerAdapter);

    }





    private RefreshListenerAdapter mRefreshListenerAdapter=new RefreshListenerAdapter() {
        @Override
        public void onRefresh(TwinklingRefreshLayout refreshLayout) {
            super.onRefresh(refreshLayout);
            switch (enterType) {
                case 3:
                    mvpPresenter.couponInfoList(useId);

                    break;


                default:
                    mPageWrapper.loadPage(true);
                    break;
            }

        }

        @Override
        public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
            super.onLoadMore(refreshLayout);

            mPageWrapper.loadPage(false);

        }
    };


    /**
     * 优惠券
     * @param mData
     */
    @Override
    public void loadCouponsData(List<Coupons> mData) {
        isloadfinish=true;
        mCouponAdapter.setDatas(mData);
    }

    @Override
    public void loadtradeOrderData(TradeRecordBean mtradebean) {
        isloadfinish=true;
        mPageWrapper.addDataSource(mtradebean.getList(),mtradebean.getPages());
    }

    @Override
    public void loadRechargeData(RechargeRecordBean mRechargebean) {
        isloadfinish=true;
        mPageWrapper.addDataSource(mRechargebean.getList(),mRechargebean.getPages());
    }


    @Override
    public void loadFail(String msg) {
        T.show(msg);
    }

    @Override
    public void loadload() {
        twinklingLayout.finishRefreshing();
        twinklingLayout.finishLoadmore();
    }
}
